package com.tjsinfo.tjsoa.fileDocument.bo;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IJDBCDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.fileDocument.dao.IFileDocumentUserDAO;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentGroup;
import com.tjsinfo.tjsoa.fileDocument.vo.FileDocumentUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class FileDocumentUserBo implements IFileDocumentUserBo {
	private IJDBCDao jdbcdao;
	private IQbcDao qbcDao;
	
	private IFileDocumentGroupBo documentGroupBo;
	private IFileDocumentUserDAO documentUserDAO;

	public void setDocumentUserDAO(IFileDocumentUserDAO documentUserDAO) {
		this.documentUserDAO = documentUserDAO;
	}

	public void setDocumentGroupBo(IFileDocumentGroupBo documentGroupBo) {
		this.documentGroupBo = documentGroupBo;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_User.IFileDocumentUserBo#setJdbcdao(com.tjsinfo.tjsoa.Dao.IJDBCDao)
	 */
	public void setJdbcdao(IJDBCDao jdbcdao) {
		this.jdbcdao = jdbcdao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_User.IFileDocumentUserBo#getDownUser(int)
	 */
	public String[] getDownUser(String groupid) {
		FileDocumentGroup group = documentGroupBo.findById(groupid);
		List<FileDocumentUser> list = documentUserDAO.findByProperty("fileDocumentGroup", group);
		String[] str = new String[list.size() * 2];
		int i = 0;
		for (FileDocumentUser user : list) {
			str[i] = user.getTjsUser().getUserName();
			i++;
		}
		return str;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_User.IFileDocumentUserBo#groupUser(java.util.List, java.util.List)
	 */
	public void groupUser(List<String> groupids, List<Integer> users) {
		try {
			String sql = "insert into dbo.t_fileDocument_user(group_id,user_id) VALUES(?,?)";
			PreparedStatement prest = jdbcdao.getConn().prepareStatement(sql);
			for (Iterator iterator = groupids.iterator(); iterator.hasNext();) {
				String groupid = (String) iterator.next();
				for (Iterator iterator2 = users.iterator(); iterator2.hasNext();) {
					Integer userid = (Integer) iterator2.next();
					prest.setString(1, groupid);
					prest.setInt(2, userid);
					prest.addBatch();
				}
			}
			prest.executeBatch();
			jdbcdao.closeConn();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.fileDocument.FileDocument_User.IFileDocumentUserBo#deleteByGroup(java.lang.String)
	 */
	public void deleteByGroup(String groups) {
		String sql = "delete from dbo.t_fileDocument_user where group_id ='"+groups+"'";
		jdbcdao.executeUpdate(sql);
		jdbcdao.closeStmt();
		jdbcdao.closeConn();
	}

	public List<FileDocumentUser> selectGroupUser(String groupid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(FileDocumentUser.class);
		criteria.add(Restrictions.eq("fileDocumentGroup.id", groupid));
		List<FileDocumentUser> list = qbcDao.getCriteria(criteria);
		return list;
	}
	
	public void addGroupUser(FileDocumentUser fileDocumentUser) {
		// TODO Auto-generated method stub
		documentUserDAO.save(fileDocumentUser);
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}
}
